package com.hengxinguotong.hxgtwygsp.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hengxinguotong.baselibrary.wheel.AbstractWheelTextAdapter;
import com.hengxinguotong.baselibrary.wheel.WheelView;
import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.model.IText;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * TODO
 *
 * @author hjz
 * @version V1.0.0
 * @since 2019/4/9 13:17
 */

public abstract class DoubleListPW<K, V>
{
    protected OnClickCallback<K, V> callback;
    protected Context context;
    private PopupWindow popupWindow;

    @BindView(R.id.left_recycler)
    WheelView leftRecycler;
    @BindView(R.id.right_recycler)
    WheelView rightRecycler;

    public DoubleListPW(Context context, int width)
    {
        this.context = context;
        View view = View.inflate(context, R.layout.popup_double_list, null);
        ButterKnife.bind(this, view);
        popupWindow = new PopupWindow(view, width, ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置焦点，必须设置，否则PopupWindow内的空间无法响应事件
        popupWindow.setFocusable(true);
        //必须设置，否则获得焦点后页面上其他地方点击无响应
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置点击其他地方 popupWindow消失
        popupWindow.setOutsideTouchable(true);
    }

    public abstract void initData(List<K> list);

    public void setOnClickCallback(OnClickCallback<K, V> callback)
    {
        this.callback = callback;
    }

    @OnClick({R.id.popup_cancel, R.id.popup_confirm})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.popup_cancel:
                popupWindow.dismiss();
                break;
            case R.id.popup_confirm:
                if (callback != null)
                {
                    onConfirm(leftRecycler.getCurrentItem(), rightRecycler.getCurrentItem());
                }
                popupWindow.dismiss();
                break;
        }
    }

    public void showAsDropDown(View view)
    {
        popupWindow.showAsDropDown(view, 0, 10);
    }

    abstract void onConfirm(int leftIndex, int rightIndex);

    public class WheelAdapter<T extends IText> extends AbstractWheelTextAdapter
    {
        private List<T> iTextList;
        private int selectColor;
        private int defaultColor;

        protected WheelAdapter(Context context, List<T> iTextList)
        {
            super(context);
            this.iTextList = iTextList;
            this.defaultColor = Color.parseColor("#B2C8D8FF");
            this.selectColor = Color.parseColor("#C8D8FF");
        }

        public void setTextList(List<T> iTextList)
        {
            this.iTextList = iTextList;
        }

        @Override
        public View getItem(int index, View convertView, ViewGroup parent)
        {
            ViewHolder holder;
            if (convertView == null)
            {
                convertView = View.inflate(context, R.layout.item_double_popup, null);
                holder = new ViewHolder();
                holder.doublePopupText = convertView.findViewById(R.id.double_popup_text);
                convertView.setTag(holder);
            } else
            {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.doublePopupText.setText(getItemText(index));
            if (getCurrentIndex() == index)
            {
                holder.doublePopupText.setTextColor(selectColor);
                holder.doublePopupText.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            } else
            {
                holder.doublePopupText.setTextColor(defaultColor);
                holder.doublePopupText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            }
            return convertView;
        }

        @Override
        public int getItemsCount()
        {
            return iTextList.size();
        }

        @Override
        protected CharSequence getItemText(int index)
        {
            return iTextList.get(index).getText();
        }

        class ViewHolder
        {
            private TextView doublePopupText;
        }
    }

    public interface OnClickCallback<K, V>
    {
        void onClickPW(K k, V v);
    }
}
